package com.easygem.controller.front;

import com.easygem.common.annotation.ControllerEndpoint;
import com.easygem.common.error.SystemException;
import com.easygem.common.model.front.Blacklist;
import com.easygem.common.response.ResponseBean;
import com.easygem.common.response.ResponseMessage;
import com.easygem.common.utils.ExcelUtils;
import com.easygem.common.vo.front.BlacklistVo;
import com.easygem.common.vo.system.PageVO;
import com.easygem.front.service.BlackListService;
import com.wuwenze.poi.ExcelKit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author luoxi
 * @version 1.0
 * @className BlacklistController
 * @Description 黑名单控制层
 * @date 2024/12/24 15:55
 */
@Api(tags = "黑名单")
@RestController
@RequestMapping("/system/blacklist")
public class BlacklistController {
    @Autowired
    private BlackListService blacklistService;
    @ApiOperation(value = "黑名单列表", notes = "模糊查询黑名单列表")
    @GetMapping("/findList")
    @ResponseBody
    public ResponseBean<PageVO<Blacklist>> findList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                    @RequestParam(value = "pageSize", defaultValue = "7") Integer pageSize,
                                                    BlacklistVo blacklistVo) {
        PageVO<Blacklist> blacklistPageVO = blacklistService.findList(pageNum, pageSize, blacklistVo);
        return ResponseBean.success(blacklistPageVO);
    }
    /**
     * 添加系统通知
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "添加黑名单失败", operation = "添加系统通知")
    @RequiresPermissions({"blacklist:add"})
    @ApiOperation(value = "添加黑名单")
    @PostMapping("/add")
    @ResponseBody
    public ResponseBean add(@RequestBody @Validated BlacklistVo blacklistVo) {
        blacklistService.add(blacklistVo);
        return ResponseBean.success();
    }
    /**
     * 删除黑名单
     *
     * @param id
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "删除黑名单失败", operation = "删除黑名单")
    @ApiOperation(value = "删除黑名单")
    @RequiresPermissions({"blacklist:delete"})
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public ResponseBean delete(@PathVariable String[] id) throws SystemException {
        if (id.length == 1) {
            blacklistService.delete(id[0]);
        }else{
            blacklistService.delByBlacklistIds(id);
        }
        return ResponseBean.success();
    }
    /**
     * 根据条件清空删除黑名单
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "根据条件清空删除黑名单失败", operation = "根据条件清空删除黑名单")
    @ApiOperation(value = "清空删除黑名单")
    @RequiresPermissions({"blacklist:erase"})
    @PostMapping("/deletes")
    @ResponseBody
    public ResponseBean deletes(@RequestBody @Validated BlacklistVo blacklistVo){
        blacklistService.delBlacklistIds(blacklistVo);
        return ResponseBean.success();
    }

    /**
     * 导出excel
     * @param response
     */
    @ApiOperation(value = "导出excel", notes = "导出所有黑名单的excel表格")
    @PostMapping("/excel")
    @RequiresPermissions("blacklist:export")
    @ControllerEndpoint(exceptionMessage = "导出Excel失败",operation = "导出黑名单excel")
    public void export(HttpServletResponse response,@RequestBody BlacklistVo blacklistVo) {
        List<Blacklist> blacklistList = this.blacklistService.findByBlacklistAll(blacklistVo);
        ExcelKit.$Export(Blacklist.class, response).downXlsx(blacklistList, false);
    }

    /**
     *  导入黑名单
     * @param file excel文件
     * @return 导入的黑名单列表
     * @throws Exception 异常
     */
    @PostMapping("/importBlacklist")
    @ResponseBody
    @RequiresPermissions("blacklist:import")
    @ControllerEndpoint(exceptionMessage = "导入黑名单失败",operation = "导入黑名单")
    public ResponseMessage importResource(@RequestParam("file") MultipartFile file,@RequestParam("accountName") String accountName) throws Exception {
        System.out.println(file.getOriginalFilename());
        //创建表格规则
        Map<String,String> map = new HashMap<>();
        map.put("客户名称","clientName");
        map.put("客户电话","clientPhone");
        map.put("备注","remark");
        //解析表格
        List<BlacklistVo> blacklistVos = ExcelUtils.parseExcel(file,BlacklistVo.class,map);
        //调用导入资源方法
        blacklistService.importBlacklist(blacklistVos,accountName);
        return ResponseMessage.success("success");
    }


}
